// pages/checkpoint/checkpoint.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zhe: false,
    ti_success: false,
    answer_success: false,
    answer_fail:false,
    yes:[0,0,0,0],
    animationData:{},
    zhe_animation:{},
    tip_num:0,
    tip_list:[],
    daojishi: 100
  },

  // 选择题目
  tip:function(e){
    console.log(e)
  
    var that = this;
    var monney = that.data.money;
    clearInterval(that.data.daoji)
    var num = that.data.tip_num;
    var index = e.target.dataset.index;
    var id = e.target.dataset.id;
    wx.request({
      url: app.globalData.url +'jingcai.php?Action=gengxintimushu',
      data:{
        A_id: that.data.tip_list[that.data.tip_num].A_id
      },
      success:function(res){
        console.log(res)
      }
    })
    that.setData({
      yes: [0, 0, 0, 0],
    })
    if(id == index){
      var yes = this.data.yes;
      yes[index-1] = "11";
      console.log(yes)
      this.setData({
        yes: yes
        
      })
      if (num == 9) {
        that.setData({
          ti_success: true,
          zhe: true,
          money1: "抽奖中..."
        })
        wx.request({
          url: app.globalData.url+'jingcai.php?Action=gengxinzhanghu',
          data:{
            openid:app.globalData.opid,
            money:that.data.money
          },
          success:function(res){
            console.log(res)
          }
        })
        setTimeout(function () {
          that.setData({
            money1: false
          })
        }, 2000)
        return false
      }
      
      setTimeout(function(){
        that.setData({
          answer_success: true,
          zhe: true
          
        })
      },500)
    
    }else{
      var yes = this.data.yes;
      yes[index-1] = "-1";
      this.setData({
        yes: yes
      })
      setTimeout(function () {
        that.setData({
          answer_fail: true,
          zhe: true
        })
      }, 500)
    }
   
  },
  
  // 取消回答正确弹出框
  answer_success_cennel:function(){
    var that = this;
    var index = that.data.tip_num+1;
    console.log(index)
    
    that.setData({
      answer_success: false,
      zhe: false,
      tip_num:index,
      yes:[0,0,0,0],
      daojishi:100
    })
    that.dao();
  },
  // 取消回答错误弹出框
  answer_fail_cennel:function () {
    var that = this;
    that.setData({
      answer_fail: false,
      zhe: false
    })
    console.log('../cai/cai')
    wx.switchTab({
      url: '../cai/cai'
    })

  },
  dao:function(){
    var that= this;
    that.data.daoji = setInterval(function () {

      var daojishi = that.data.daojishi;

      console.log(daojishi);
      if (daojishi <= 0) {
        clearInterval(that.data.daoji)
        console.log(daojishi)
        that.setData({
          answer_fail: true,
          zhe: true
        })
      }
      that.setData({
        daojishi: daojishi - 10
      })
    }, 1000)
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: app.globalData.url + 'jingcai.php?Action=srecord',
      data: {
        openid: app.globalData.opid,
        R_status: options.id
      },
      success: function (res) {
        console.log(res)
        console.log(app.globalData.opid)
        console.log(options.id)
        that.setData({
          R_status:options.id
        })
      }
    })
   
    

    that.data.daoji = setInterval(function () {

      var daojishi = that.data.daojishi;
     
      console.log(daojishi);
      if (daojishi <= 0) {
        clearInterval(that.data.daoji)
        that.setData({
          answer_fail:true,
          zhe:true
        })
      }
      that.setData({
        daojishi: daojishi - 10
      })
    }, 1000)


    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: "linear",
      delay: 0
    })
   

    this.animation = animation
    animation.backgroundColor('019801').step() ;
    
    
    this.setData({
      animationData: animation.export()
    })

    // 获取题目
    wx.request({
      url: app.globalData.url+'jingcai.php?Action=jingcailist',
      
      success:function(res){
        console.log(res)
        that.setData({
          tip_list:res.data
        })
      }
    })
    // 抽奖金额
    wx.request({
      url: app.globalData.url+'jingcai.php?Action=choujiangjiner',
      success:function(res){
        that.setData({
          money:res.data
        })
      }
    })
  },
 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  tiao_cennel: function () {
    var that = this;
    that.setData({
      ti_success: false,
      zhe: false
    })
    wx.switchTab({
      url: '../cai/cai',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  //分享
  onShareAppMessage: function (res) {
    let that = this
    return {
      title: '有奖竞猜',
      path: '/pages/share/share'
    }
    wx.switchTab({
      url: '../cai/cai'
    })
  }

})